package self.hoy.demo.rabbitmq.ps;

import com.rabbitmq.client.*;
import self.hoy.demo.rabbitmq.Constant;
import self.hoy.demo.rabbitmq.util.ConnectionUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发布订阅模式
 * @author hyh
 * @date 2020/6/7
 */
public class ShortMessageServiceReceive implements Constant {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(SMS_QUEUE_NAME, false, false, false, null);
        String routingKey = "";
        //绑定队列交换机/转发器
        channel.queueBind(SMS_QUEUE_NAME, FANOUT_EXCHANGE_NAME, routingKey);
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,
                                       AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("==============[sms] receive message : " + new String(body));
            }
        };
        channel.basicConsume(SMS_QUEUE_NAME, true, consumer);
//        channel.close();
//        connection.close();
    }
}
